<template>
  <div class="xpage">
    <!-- 查询表单 -->
    <ele-form-search
      :formConfig="formConfig"
      :initForm="initForm"
      @handleSearch="handleSearch"
      @handleReset="handleReset"
      ref="eleFormSearch"
    ></ele-form-search>
    <slot></slot>
    <!-- 列表展示 -->
    <ele-table
      ref="xTable"
      v-bind="$attrs"
      v-on="$listeners"
      :beforeRequest="beforeRequest"
      @handleOperateEvent="handleOperateEvent"
    >
    </ele-table>
  </div>
</template>
<script>
import eleFormSearch from "@/components/ele-form-search/index";
import { searchArrKey } from "@/mixins/searchArrKey";
import { exportExcel } from "@/utils/judge";
export default {
  inheritAttrs: false,
  name: "xpage",
  mixins: [searchArrKey],
  components: { eleFormSearch },
  props: {
    formConfig: {
      type: Object,
      default: () => ({}),
    },
    beforeAjax: Function,
    //初始化时form的值
    initForm: {
      type: Object,
      default: () => ({}),
    },
    //其他参数
    otherParams: {
      type: Object,
      default: () => ({}),
    },
  },

  methods: {
    handleSearch() {
      const { handleSearch } = this.$listeners;
      handleSearch
        ? this.$emit("handleSearch", this.getSearchParams())
        : this.refresh(true);
    },
    handleReset() {
      const { handleReset } = this.$listeners;
      handleReset ? this.$emit("handleReset") : this.refresh(true);
    },
    getSearchParams() {
      let formData = this.$refs.eleFormSearch.formData || {};
      formData = this.getArrtoMap(formData);
      return formData;
    },
    beforeRequest(data) {
      let formData = this.$refs.eleFormSearch.formData || {};
      formData = this.getArrtoMap(formData);
      const params = { ...data, ...this.otherParams, ...formData };
      return this.beforeAjax ? this.beforeAjax(params) : params;
    },
    refresh(isReset = true) {
      this.$nextTick(() => {
        this.$refs.xTable.getTableData(isReset);
      });
    },
    handleOperateEvent(data) {
      if (data.handleName === "export" && data.url) {
        const params = { ...this.getSearchParams(), ...this.otherParams };
        exportExcel(data.url, params);
      }
    },
  },
};
</script>
<style lang="scss" scoped></style>
